package cn.hesion.rocketmqdemo.cn.hesion.controller;
/**
 * ClassName: MQSenderController <br/>
 * Description: <br/>
 * date: 2021/11/5 13:34<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * 生产者发送消息Controller
 * @author: hesion
 * @create: 2021-11-05 13:34
 **/
@RestController
@Slf4j
public class MQSenderController {

    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @RequestMapping(value ="/sendMsg/{mq}",method = RequestMethod.GET)
    public String sendMsg(@PathVariable String mq) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        log.info("发送消息:"+mq);
        Message message = new Message("TestTopic", "TestTag", mq.getBytes());
        SendResult sendResult = defaultMQProducer.send(message);
        log.info("消息发送响应：" + sendResult.toString());
        return "OK";
    }
}
